var express = require('express');
var router = express.Router();
var path = require("path");
var addrServ = require(path.join(process.cwd(), "/services/AddressService"));

/**
 * 【增】单个添加地址
 * this.$http.post('/insert',{
 *     user_id: "", receive_name: "", receive_tel: "", receive_address_full: "",
 *     area_code: "", is_default: "",
 * })
 * 
 */
router.post("/insert", function (req, res, next) {
	addrServ.createAddress({
		"user_id": req.userInfo.uid,
		"receive_name": req.body.receive_name,
		"receive_tel": req.body.receive_tel,
		"receive_address_full": req.body.receive_address_full,
		"receive_address_detail": req.body.receive_address_detail,
		"area_code": req.body.area_code,
		"is_default": req.body.is_default
	}, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

/** 
 * 【删】单个物理删除
 * this.$http.delete('/delete/12')
 */
router.delete("/delete/:id", function (req, res, next) {
	addrServ.deleteAddress(req.params.id, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

/** 
 * 【删】批量物理删除
 * this.$http({
 *     method:'delete',
 *     url:'/batchDelete',
 * 	   data:{
 * 		 ids:[]
 * 	   }
 * })
 */
router.delete("/batchDelete", function (req, res, next) {
	addrServ.deleteAddress(req.body.ids, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

/** 
 * 【改】编辑地址
 * this.$http.put('/update/' + item.id,{
 *     is_default:  true | false,
 * })
 */
router.put("/update/:id", function (req, res, next) {
	addrServ.updateAddress(req.params.id, {
		user_id: req.userInfo.uid,
		...req.body
	}, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});


/** 
 * 【查】分页查询
 * this.$http({
 *		url:'/getPage', 
 *		method:'post',
 *		params: this.queryInfo,
 * })
 */
router.post("/getPage", function (req, res, next) {
	var conditions = {
		"where": {
			user_id: req.userInfo.uid
		},
		"pagenum": req.query.pagenum,
		"pagesize": req.query.pagesize
	}
	addrServ.getAllAddresss(conditions, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

/** 
 * 【查】根据 ID 查询
 * this.$http.get('/getByID/' + item.id)
 */
router.get("/getByID/:id", function (req, res, next) {
	addrServ.getAddressById(req.params.id, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

/** 
 * 【查】搜索查询
 * this.$http({
 *		url:'/getDefault', 
 *		method:'get',
 * })
 */
router.get("/getDefault", function (req, res, next) {
	var conditions = {
		user_id: req.userInfo.uid,
		is_default: 1
	}
	addrServ.getDefaultAddress(conditions, function (err, result) {
		if (err) return res.sendResult(null, err.code, err.msg, err.stack);
		res.sendResult(result.data, result.code, result.msg, null);
	});
});

module.exports = router;